package com.version2_0.service.implement;

import com.util.aliyun.OCSutils;
import com.util.encrypt.TokenEncrypt;
import com.util.format.ReevesMap;
import com.version1_1.dao.UserDao;
import com.version1_3.dao.ShareDao;
import com.version1_3.dao.UserDao1_3;
import com.version1_3.model.UserModel;
import com.version2_0.service.UserService2_0;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * Created by reeveslee on 16/6/18.
 */
@Service
public class UserServiceImpl2_0 implements UserService2_0 {
    private static final Logger LOGGER = Logger.getLogger(UserServiceImpl2_0.class);

    @Autowired
    private UserDao1_3 userDao13;

    @Autowired
    private UserDao userDao;

    @Autowired
    private ShareDao shareDao;

    /**
     * 第三方注册,获取kbToken
     *
     * @param platform
     * @param os
     * @param openid
     * @param token
     * @return
     */
    @Override
    public String kbToken(String platform, int os, String openid, String token) {
        ReevesMap reevesMap = new ReevesMap();
        String ourOS = (os == 0) ? "iOS" : "Android";
        OCSutils ocSutils = new OCSutils();

        Map<String, Object> userInfoMap = userDao13.getUserInfoWithThirdPartLogin(openid, ourOS);

        if (userInfoMap == null) {
            // 第三方注册新账号
            reevesMap.setCode(401);
            reevesMap.setInfo("user not register,please register first.");
        } else {
            // 用户刷新kbToken
            int userId = (int) userInfoMap.get("userId");
            userDao13.freshUserToken(userId, token);
            String kbTokenStr = TokenEncrypt.encrpt(userId + token + new Date().getTime() + "");
            ocSutils.delete("kbToken" + userId);
            ocSutils.save("kbToken" + userId, kbTokenStr, 15 * 24 * 3600);
            reevesMap.put("id", userId);
            reevesMap.put("kbToken", kbTokenStr);
        }

        return reevesMap.toJsonString();
    }


    /**
     * 手机号注册用户获取kbToken
     *
     * @param telnumber
     * @param passwd
     * @return
     */
    @Override
    public String kbToken(String telnumber, String passwd) {
        boolean isUserExist = userDao.checkUserExist("", telnumber);
        ReevesMap reevesMap = new ReevesMap();

        // 检测到用户不存在
        if (!isUserExist) {
            reevesMap.setCode(401);
            reevesMap.setInfo("user not register,please register first.");
        } else {
            UserModel userModel = userDao13.login(-1, telnumber, passwd);
            if (userModel == null) {
                // 密码错误,登陆失败
                reevesMap.setCode(401);
                reevesMap.setInfo("login error,password is not correct.");
            } else {
                String kbTokenStr = "";
                OCSutils ocSutils = new OCSutils();
                if (ocSutils.get("kbToken" + userModel.getUserId()) != null) {
                    kbTokenStr = (String) ocSutils.get("kbToken" + userModel.getUserId());
                } else {
                    kbTokenStr = TokenEncrypt.encrpt(userModel.getUserId() + passwd + new Date().getTime() + "");
                    ocSutils.save("kbToken" + userModel.getUserId(), kbTokenStr, 15 * 24 * 3600);
                }

                reevesMap.put("id", userModel.getUserId());
                reevesMap.put("kbToken", kbTokenStr);
            }
        }

        return reevesMap.toJsonString();
    }
}
